package com.shilan.dllo.wangyi.activity;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TabHost;

import com.shilan.dllo.wangyi.R;
import com.shilan.dllo.wangyi.fragment.NewsFragment;
import com.shilan.dllo.wangyi.fragment.PcFragment;

import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

    private TabHost tabHost;

    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//调用方法
        initView();
        createTabSpec();
        replaceFragment();
    }

    /***
     * 初始化控件
     */
    public void initView() {
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        fragmentManager = getSupportFragmentManager();

    }

    /***
     * 创建标签
     */
    public void createTabSpec() {

        //创建第一个标签
        TabHost.TabSpec news = tabHost.newTabSpec("新闻");
        View viewNews = getLayoutInflater().inflate(R.layout.tab_news , null);
        news.setIndicator(viewNews);
        news.setContent(R.id.newsView);
        tabHost.addTab(news);

        //创建第一个标签
        TabHost.TabSpec read = tabHost.newTabSpec("阅读");
        View viewRead = getLayoutInflater().inflate(R.layout.tab_read,null);
        read.setIndicator(viewRead);
        read.setContent(R.id.readView);
        tabHost.addTab(read);

        TabHost.TabSpec va = tabHost.newTabSpec("视听");
        View viewVa = getLayoutInflater().inflate(R.layout.tab_va,null);
        va.setIndicator(viewVa);
        va.setContent(R.id.vaView);
        tabHost.addTab(va);

        TabHost.TabSpec pc = tabHost.newTabSpec("阅读");
        View viewPc = getLayoutInflater().inflate(R.layout.tab_pc,null);
        pc.setIndicator(viewPc);
        pc.setContent(R.id.pcView);
        tabHost.addTab(pc);

    }

    /**
     * 替换tabhost中的占位布局
     */
    public void replaceFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.newsView, new NewsFragment());
        transaction.replace(R.id.pcView, new PcFragment());
        transaction.commit();

    }
}
